package com.photo.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photo.system.annotation.AuthCheck;
import com.photo.system.common.BaseResponse;
import com.photo.system.common.DeleteRequest;
import com.photo.system.common.ErrorCode;
import com.photo.system.common.ResultUtils;
import com.photo.system.constant.UserConstant;
import com.photo.system.exception.BusinessException;
import com.photo.system.exception.ThrowUtils;
import com.photo.system.model.dto.notifications.NotificationsAddRequest;
import com.photo.system.model.dto.notifications.NotificationsQueryRequest;
import com.photo.system.model.dto.notifications.NotificationsUpdateRequest;
import com.photo.system.model.dto.user.*;
import com.photo.system.model.entity.Notifications;
import com.photo.system.model.entity.User;
import com.photo.system.model.vo.LoginUserVO;
import com.photo.system.model.vo.UserVO;
import com.photo.system.service.NotificationsService;
import com.photo.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 通知接口
 *
 * @author xuwenhui
 */
@RestController
@RequestMapping("/notification")
@Slf4j
@Api(tags = {"通知接口"})
public class NotificationsController {

    @Resource
    private UserService userService;

    @Resource
    private NotificationsService notificationsService;

    // region 增删改查

    /**
     * 创建通知(仅管理员)
     *
     * @param notificationsAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "创建通知")
    public BaseResponse<Long> addNotifications(@RequestBody NotificationsAddRequest notificationsAddRequest, HttpServletRequest request) {
        if (notificationsAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Notifications notifications = new Notifications();
        BeanUtils.copyProperties(notificationsAddRequest, notifications);
        boolean result = notificationsService.save(notifications);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(notifications.getId());
    }

    /**
     * 删除通知
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除通知")
    public BaseResponse<Boolean> deleteNotification(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = notificationsService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "删除失败");
        return ResultUtils.success(result);
    }

    /**
     * 更新通知
     *
     * @param notificationsUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "更新通知")
    public BaseResponse<Boolean> updateNotification(@RequestBody NotificationsUpdateRequest notificationsUpdateRequest) {
        if (notificationsUpdateRequest == null || notificationsUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Notifications notifications = new Notifications();
        BeanUtils.copyProperties(notificationsUpdateRequest, notifications);
        boolean result = notificationsService.updateById(notifications);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新失败");
        return ResultUtils.success(true);
    }

    /**
     * 分页获取全部通知
     *
     * @param notificationsQueryRequest
     * @return
     */
    @PostMapping("/get/all")
    @ApiOperation("分页获取全部通知")
    public BaseResponse<Page<Notifications>> listUserByPage(@RequestBody NotificationsQueryRequest notificationsQueryRequest) {
        QueryWrapper<Notifications> queryWrapper = new QueryWrapper<>();

        long current = notificationsQueryRequest.getCurrent() == 0 ? 1 : notificationsQueryRequest.getCurrent();
        long size = notificationsQueryRequest.getPageSize() == 0 ? 10 : notificationsQueryRequest.getPageSize();

        Page<Notifications> pageParam = new Page<>(current, size);

        // 调用服务层方法获取分页数据
        Page<Notifications> notifications = notificationsService.page(pageParam, queryWrapper);
        return ResultUtils.success(notifications);
    }
}
